package com.raymond.queue.codec.decoding;

import com.raymond.queue.common.codec.AgreementDecoder;
import com.raymond.queue.common.codec.BaseCodecStrategy;
import com.raymond.queue.common.codec.annotation.MsgCodec;
import com.raymond.queue.common.codec.decoding.BaseDecoder;
import com.raymond.queue.common.enums.CommandIdType;
import com.raymond.queue.common.msg.BaseAgreement;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;

import java.util.List;

/**
 * 打包请求解码
 *
 * @author :  raymond
 * @version :  V1.0
 * @date :  2021-07-14 14:24
 */
@MsgCodec(commandId = CommandIdType.PACK_REQ)
public class PackRequestDecoder extends BaseDecoder {
    @Override
    public void decode(ChannelHandlerContext ctx, BaseAgreement msg, List<Object> out) throws Exception {
        ByteBuf bodyBuffer = Unpooled.wrappedBuffer(msg.getBodyBuffer());
        int size = bodyBuffer.readInt();
        for (int i = 0; i < size; i++) {
            BaseAgreement baseAgreement = AgreementDecoder.byteBuf2BaseAgreement(bodyBuffer);
            BaseCodecStrategy.getBaseDecoder(baseAgreement.getCommandId()).decode(ctx, baseAgreement, out);
        }
    }
}
